package targetOffer;

public class IntegerToHex {
    public static void main(String[] args) {
        System.out.print(getPatternScore("11"));
//        System.out.println(myPow(2, 10));
    }

    //越接近11111的分数越高
    private static int getPatternScore(String pattern) {

        int[] array = analysePattern(pattern);
        int score = 0;
        int zeroCount = 0;
        //奇数位是1的个数,偶数为0的个数
        for (int i = 0; i < array.length; i++) {
            if (array[i] != 0) {
                if (i % 2 == 0) {
                    if (zeroCount != 0) {
                        int self = myPow(8, array[i] - 1);//该位置的分数
                        score = (score + self) * (4 - zeroCount);
                    } else {
                        score += myPow(8, array[i] - 1);
                        zeroCount = 0;
                    }
                } else {
                    zeroCount = array[i];
                }
            }
        }
        return score;
    }

    private static int myPow(int base, int pow) {
        int result = 1;
        while (pow > 0) {
            result = result * base;
            pow--;
        }
        return result;
    }

    private static int[] analysePattern(String pattern) {
        int[] countOneAndZero = new int[9];

        int count1 = 0;
        int count0 = 0;
        boolean isOne = true;
        int index = 0;
        for (int i = 0; i < pattern.length(); i++) {
            if (pattern.charAt(i) == '1') {
                if (!isOne) {
                    //0
                    countOneAndZero[index] = count0;
                    count0 = 0;
                    index++;
                }
                isOne = true;
                count1++;
            } else if (pattern.charAt(i) == '0') {
                if (isOne) {
                    countOneAndZero[index] = count1;
                    count1 = 0;
                    index++;
                }
                isOne = false;
                count0++;
            }
        }

        countOneAndZero[index] = count1;
        return countOneAndZero;
    }
}
